
const dao = require("../dao/upvote")
const response = require("../utils/response")
const { checkauth } = require("../utils/check");

/**
 * 点赞
 * @param {*} data {secret_id, user_id, target_id, type}
 */
async function add_upvote(data) {
    let auth = await checkauth(data.secret_id, data.user_id);
    if (!auth) {
        return response.forbidden("权限错误");
    }
    if (data.type == 'post') {
        await dao.add_upvote_post(data.user_id, data.target_id);
    } else {
        await dao.add_upvote_comment(data.user_id, data.target_id);
    }
}

/**
 * 取消点赞
 * @param {*} data {secret_id, user_id, target_id, type}
 */
async function del_upvote(data) {
    let auth = await checkauth(data.secret_id, data.user_id);
    if (!auth) {
        return response.forbidden("权限错误");
    }
    if (data.type == 'post') { 
        await dao.del_upvote_post(data.user_id, data.target_id);
    } else {
        await dao.del_upvote_comment(data.user_id, data.target_id);
    }
}

module.exports = {
    add_upvote: add_upvote,
    del_upvote: del_upvote,
}
